package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {
    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private Category category;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Page<DishDto> page(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);

        List<Dish> dishList=dishMapper.findByName(page,pageSize,name);

        PageInfo<Dish> pageInfo = new PageInfo<>(dishList);

        List<DishDto> collect = pageInfo.getList().stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            Category category = categoryMapper.findById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            return dishDto;
        }).collect(Collectors.toList());
        Page<DishDto> dishPage = new Page<>(collect, pageInfo.getTotal(), pageInfo.getPageNum(), pageInfo.getPageSize());
        return dishPage;
    }

    @Override
    @Transactional
    public void saveWithFlavors(DishDto dishDto) {
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setStatus(1);
        dishDto.setSort(0);
        dishDto.setCategoryId(dishDto.getCategoryId());
        dishMapper.saveWithFlavors(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            flavor.setDishId(dishDto.getId());
        }
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        dishFlavorMapper.saveWithFlavors(flavors);


    }

    @Override
    public DishDto selectById(Long id) {
        Dish dish=dishMapper.selectById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        List<DishFlavor> dishFlavor=dishFlavorMapper.selectById(id);
        dishDto.setFlavors(dishFlavor);
        return dishDto;
    }

    @Override
    @Transactional
    public void update(DishDto dishDto) {
        dishDto.setUpdateTime(LocalDateTime.now());
        dishMapper.update(dishDto);
        dishFlavorMapper.deleteWithFlavor(dishDto.getId());
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            flavor.setDishId(dishDto.getId());
        }
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        dishFlavorMapper.saveWithFlavors(flavors);
    }

    @Override
    public List<DishDto> findByIdlist(Long categoryId, Integer status) {
        List<Dish> dishList=null;
        String keys="dish_"+categoryId+"status_"+status;
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<DishDto> dishListDto = (List<DishDto>) valueOperations.get(keys);
        if(dishList==null){
            dishList=dishMapper.findByIdlist(categoryId,status);
            dishListDto = dishList.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            Category category = categoryMapper.findByCategoryId(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            List<DishFlavor> flavors = dishFlavorMapper.selectById(dish.getId());
            dishDto.setFlavors(flavors);
            return dishDto;
        }).collect(Collectors.toList());
            valueOperations.set(keys,dishList,60*24, TimeUnit.MINUTES);
        }
        return dishListDto;
    }

    @Override
    public void deletebatch(List<Long> ids) {
        Long count=dishMapper.queryStatusBatch(ids);
        if(count>0){
            throw new CustomException("不能批量删除");
        }
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByIds(ids);
    }

    @Override//批量停售
    public void updateStatusZero(List<Long> ids) {
        Long empId = (Long) session.getAttribute("employee");
        LocalDateTime updateTime =LocalDateTime.now();
        dishMapper.updateStatusZero(ids,empId,updateTime);
    }

    @Override
    public void updateStatusOne(List<Long> ids) {
        Long empId = (Long) session.getAttribute("employee");
        LocalDateTime updateTime =LocalDateTime.now();
        dishMapper.updateStatusOne(ids,empId,updateTime);
    }
}
